package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * Created by lenovo on 2016/4/27.
 */
public class PersonalOrAssignSearchFragment extends Fragment implements View.OnClickListener {
  protected static final String EXTRA_STARTTIME =
      "com.isunland.managesystem.ui.PersonalOrAssignSearchFragment.EXTRA_STARTTIME";
  protected static final String EXTRA_ENDTIME =
      "com.isunland.managesystem.ui.PersonalOrAssignSearchFragment.EXTRA_ENDTIME";
  protected static final String EXTRA_AFTERENDTIME =
      "com.isunland.managesystem.ui.PersonalOrAssignSearchFragment.EXTRA_AFTERENDTIME";
  protected static final String EXTRA_DATE =
      "com.isunland.managesystem.ui.PersonalOrAssignSearchFragment.EXTRA_DATE";

  private static final int START_DATE = 0;
  private static final int END_DATE = 1;

  private TextView tvPersonTitle;
  private TextView tvPerson;
  private TextView tvStartDate;
  private ImageView ibStartDate;
  private TextView tvEndDate;
  private ImageView ibEndDate;

  /* 区分是个人积分  还是分派积分  */
  public static final String EXTRA_TYPE =
      "com.isunland.managesystem.adapter.PersonalScoreListAdapter.EXTRA_TYPE";
  public static final String PERSONAL = "personal";
  public static final String ASSIGN = "assign";
  private String type;
  private String mStartTime;
  private String mEndTime;
  //    private String mAfterEndTime;
  private Date date;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.sift);
    mStartTime = getArguments().getString(EXTRA_STARTTIME);
    date = (Date) getArguments().getSerializable(EXTRA_DATE);
    mEndTime = MyDateUtil.dateToString(date, MyDateUtil.TYPE_DATE);
    type = getArguments().getString(EXTRA_TYPE);
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_score_query, container, false);
    findView(view);
    initData();
    return view;
  }

  private void findView(View view) {
    tvPersonTitle = (TextView) view.findViewById(R.id.tv_personTitle);
    tvPerson = (TextView) view.findViewById(R.id.tv_person);
    tvStartDate = (TextView) view.findViewById(R.id.tv_start_date);
    ibStartDate = (ImageView) view.findViewById(R.id.ib_start_date);
    tvEndDate = (TextView) view.findViewById(R.id.tv_end_date);
    ibEndDate = (ImageView) view.findViewById(R.id.ib_end_date);
    ibStartDate.setOnClickListener(this);
    ibEndDate.setOnClickListener(this);
  }

  private void initData() {
    if (type.equals(PERSONAL)) {
      tvPersonTitle.setText(R.string.scoredStaff);
    }
    if (type.equals(ASSIGN)) {
      tvPersonTitle.setText(R.string.scoreStaff);
    }
    tvPerson.setText(CurrentUser.newInstance(getActivity()).getRealName());
    tvStartDate.setText(mStartTime);
    tvEndDate.setText(mEndTime);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      case R.id.menu_item_confirm:
        confirm();
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  private void confirm() {

    Intent i = new Intent();
    i.putExtra(EXTRA_STARTTIME, mStartTime);
    //        i.putExtra(EXTRA_ENDTIME,mAfterEndTime);
    i.putExtra(EXTRA_DATE, date);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.ib_start_date:
        showDialog(START_DATE);
        break;
      case R.id.ib_end_date:
        showDialog(END_DATE);
        break;
      default:
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog = null;
    switch (requestCode) {
      case START_DATE:
        dialog = BaseYMDTimeDialogFragment.newInstance(MyDateUtil.stringToDate(mStartTime), "");
        break;
      case END_DATE:
        dialog = BaseYMDTimeDialogFragment.newInstance(MyDateUtil.stringToDate(mEndTime), "");
        break;
      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == START_DATE) {
      Date date = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mStartTime = MyDateUtil.dateToString(date, MyDateUtil.TYPE_DATE);
      tvStartDate.setText(mStartTime);
    }
    if (requestCode == END_DATE) {
      //所选时间
      Date mdate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mEndTime = MyDateUtil.dateToString(mdate, MyDateUtil.TYPE_DATE);
      LogUtil.i("mEndTime===" + mEndTime);
      tvEndDate.setText(mEndTime);

      //所选推后一天时间
      date = MyDateUtil.getAfterDay(mdate, 1);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static PersonalOrAssignSearchFragment newInstance(String startDate, String endDate,
      String afterEndDate, String type) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_STARTTIME, startDate);
    bundle.putString(EXTRA_ENDTIME, endDate);
    bundle.putString(EXTRA_AFTERENDTIME, afterEndDate);
    bundle.putString(EXTRA_TYPE, type);
    PersonalOrAssignSearchFragment fragment = new PersonalOrAssignSearchFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  public static PersonalOrAssignSearchFragment newInstance(String startDate, Date date,
      String type) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_STARTTIME, startDate);
    bundle.putSerializable(EXTRA_DATE, date);
    bundle.putString(EXTRA_TYPE, type);
    PersonalOrAssignSearchFragment fragment = new PersonalOrAssignSearchFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
